package us.nchu.tomtiger.controller;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import us.nchu.tomtiger.enums.ResultEnum;
import us.nchu.tomtiger.model.Result;
import us.nchu.tomtiger.model.User;
import us.nchu.tomtiger.model.UserPending;
import us.nchu.tomtiger.model.UserShow;
import us.nchu.tomtiger.service.UserPendingService;
import us.nchu.tomtiger.service.UserService;
import us.nchu.tomtiger.service.UserShowService;
import us.nchu.tomtiger.utils.ResultUtil;
import us.nchu.tomtiger.utils.UserUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by XiongXuan on 2018/1/8.
 */
@Controller
@RequestMapping("/user")
public class UserShowController {
    @Resource
    private UserShowService userShowService;
    @Resource
    private UserService userService;
    @Resource
    private UserPendingService userPendingService;

    @GetMapping("/{userShowId}")
    public ModelAndView doGet(@PathVariable String userShowId) {
        UserShow userShow;

        try {
            userShow = userShowService.getUserShowByShowId(userShowId);
        }catch (EmptyResultDataAccessException e) {
            // 找不到这个userShow id
            return new ModelAndView("user-404");
        }

        User user = userService.getUserByPhoneNumber(userShow.getPhoneNumber());

        System.out.println(user);

        ModelAndView modelAndView = new ModelAndView("user");
        modelAndView.addObject("nickName", user.getNickName());
        modelAndView.addObject("userType", user.getUserType());
        modelAndView.addObject("contactType", user.getContact_type());
        modelAndView.addObject("contact", user.getContact());
        modelAndView.addObject("intro", user.getIntro());
        modelAndView.addObject("major", user.getMajor());
        modelAndView.addObject("schoolName", user.getSchoolName());
        return modelAndView;
    }

    @PostMapping
    @ResponseBody
    // 修改用户信息
    public Result updateUserInfo(HttpServletRequest httpServletRequest, User user) {
        String originalUserType = (String) httpServletRequest.getSession().getAttribute("originalUserType");
        if (originalUserType == null) {
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_USER_METHOD_ILLEGAL);
        }

        if (Integer.parseInt(originalUserType) == user.getUserType()) {
            // 没有修改账户类型，不需要管理员审核
            userService.updateUserInfo(user);
            return ResultUtil.success("修改成功");
        }
        else {
            // 修改了账户类型，需要管理员审核
            userPendingService.createUserPending(new UserPending(user.getPhoneNumber(), user.getUserType()));
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_USER_TYPE_MODIFYED);
        }
    }
}
